package com.tank5.tank;

import java.util.Vector;

public class EnemyTank extends Tank implements Runnable{
    private int step=30;
    //敌人坦克子弹用vector保存多个子弹
    Vector<Shot> shots=new Vector<>();
    public EnemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {//敌人坦克动起来
        while(isLive()) {
            //发射子弹
            if(isLive()&&shots.size()<1){
               Shot shot=null;
               //判断坦克方向,创建子弹
                switch (getDirect()){
                    case 0:
                        shot=new Shot(getX()+20,getY()-10,0);
                        break;
                    case 1:
                        shot =new Shot(getX()+70,getY()+20,1);
                        break;
                    case 2:
                        shot=new Shot(getX()+20,getY()+70,2);
                        break;
                    case 3:
                        shot=new Shot(getX()-10,getY()+20,3);
                        break;
                }
                shots.add(shot);
                new Thread(shot).start();
            }

            //坦克移动
            switch (getDirect()) {
                case 0://上
                    for (int i = 0; i < step; i++) {
                        if(getY()>0) {
                            moveUp();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < step; i++) {
                        if(getX()+60<1000) {
                            moveRight();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < step; i++) {
                        if(getY()+60<750) {
                            moveDown();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < step; i++) {
                        if(getX()>0) {
                            moveLeft();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
            }
            //判断是否活着
//            if(!isLive){
//                System.out.println("我挂了!!!!");
//                break;
//            }
            //移动后随机改变坦克方向
            setDirect((int) (Math.random() * 4));

        }
    }
}
